package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.service.DynamicService;
import com.wing.purchase.dao.PurchaseQuotationDao;
import com.wing.purchase.model.entity.PurchasePlanDetail;
import com.wing.purchase.model.entity.PurchaseQuotation;
import com.wing.purchase.model.entity.PurchaseQuotationDetail;
import com.wing.purchase.model.request.PurchaseQuotationForm;
import com.wing.purchase.model.response.PurchaseQuotationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报价单-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PurchaseQuotationServiceImpl extends ServiceImpl<PurchaseQuotationDao, PurchaseQuotation> implements PurchaseQuotationService {

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private PurchaseQuotationDetailService purchaseQuotationDetailService;

    @Override
    @Transactional(readOnly = false)
    public int add(PurchaseQuotationForm purchaseQuotationForm) {

        //校验

        PurchaseQuotation purchaseQuotation =new PurchaseQuotation();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PurchaseQuotation.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(purchaseQuotationForm, purchaseQuotation, copyOptions);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code = dynamicService.getMaxCode("quotation_no", "pur_purchase_quotation");
        String day = simpleDateFormat.format(new Date());
        purchaseQuotation.setQuotationNo("PQ"+day+code);
        purchaseQuotation.setStatus(PurchaseQuotation.Status.confirm);
        int i = baseMapper.insert(purchaseQuotation);
        if(i > 0){
            for(PurchaseQuotationDetail purchaseQuotationDetail :purchaseQuotationForm.getPurchaseQuotationDetails()){
                purchaseQuotationDetail.setPurchaseQuotationId(purchaseQuotation.getId());
                purchaseQuotationDetail.setSupplierId(purchaseQuotation.getSupplierId());
                //计算 除税价 = 含税单价 * 100 / （税率 + 100）
                BigDecimal offerPrice = purchaseQuotationDetail.getOfferTaxPrice().multiply(new BigDecimal(100))
                        .divide(purchaseQuotationDetail.getRate().add(new BigDecimal(100)), 2, BigDecimal.ROUND_HALF_UP);
                purchaseQuotationDetail.setOfferPrice(offerPrice);
            }
            purchaseQuotationDetailService.saveBatch(purchaseQuotationForm.getPurchaseQuotationDetails());
        }
        return i;
    }

    @Override
    public List<PurchaseQuotationVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PurchaseQuotationVO> pageList4app(Page page, Map<String, Object> params) {
        List<PurchaseQuotationVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PurchaseQuotationVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int confirm(Long id) {
        PurchaseQuotation purchaseQuotation = baseMapper.selectById(id);
        purchaseQuotation.setStatus(PurchaseQuotation.Status.finish);
        return baseMapper.updateById(purchaseQuotation);
    }

    @Override
    public int cancel(Long id) {
        PurchaseQuotation purchaseQuotation = baseMapper.selectById(id);
        purchaseQuotation.setStatus(PurchaseQuotation.Status.confirm);
        return baseMapper.updateById(purchaseQuotation);
    }

    @Override
    public int reject(Long id) {
        PurchaseQuotation purchaseQuotation = baseMapper.selectById(id);
        purchaseQuotation.setStatus(PurchaseQuotation.Status.reject);
        return baseMapper.updateById(purchaseQuotation);
    }
}
